//////////////////////////////////////////////////////////////////////////
// FileName:ucDateTextBox
// CreateBy:DragonXiong
// CreateDate:2009-6-18
// Description:
//		DateTextBox usercontrol, 
//      
// Modify Log:090811 Add ClearDate Method --Fenky
//	
// Copyright:
/////////////////////////////////////////////////////////////////////////
using System;
using System.Web.UI;
using System.ComponentModel;

namespace BaseSystem._System.UserControl
{
    public enum DateTextBoxStyle { WithoutImg, WihtImgInside,WithImgOutside} //Style of DateTextBox



    [ValidationPropertyAttribute("CalendarDateString")]
    public partial class ucDateTextBox : System.Web.UI.UserControl
    {    
        string _outscript = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitPage();
            }

        }
        private void InitPage()
        {
            litJS.Text = "<script language=\"javascript\" src='" + ResolveUrl("DatePicker/WdatePicker.js") + "' type=\"text/javascript\"></script>";
            InitControl();
        }

        public void InitControl()
        {
            _outscript = "el:$dp.$('" + getClientID() + "')";
            if (!string.IsNullOrEmpty(DateFormat))
            {
                _outscript += "," + "dateFmt:'" + DateFormat + "'";
            }
            if (ShowWeek)
            {
                _outscript += ",isShowWeek:true";
            }

            switch (ClickStyle)
            {
                case DateTextBoxStyle.WihtImgInside:
                    txtDate.CssClass = "Wdate";
                    img.Visible = false;
                    break;
                case DateTextBoxStyle.WithoutImg:
                    txtDate.CssClass = "";
                    img.Visible = false;
                    break;
                case DateTextBoxStyle.WithImgOutside:
                    txtDate.CssClass = "";
                    img.Visible = true;
                    break;
                default:
                    txtDate.CssClass = "Wdate";
                    break;
            }

            _outscript+=",vel:'"+hfDate.ClientID+"'"; 


            if (ClickStyle == DateTextBoxStyle.WithImgOutside)
            {
                txtDate.Attributes.Remove("onclick");
                img.Attributes.Add("onclick","WdatePicker({"+_outscript + "})");
            }
            else
            {
                img.Attributes.Remove("onclick");
                txtDate.Attributes.Add("onclick", "WdatePicker({" + _outscript + "})");
            }
        }

        /// <summary>
        ///  Get the id of the control rendered on client side,Very essential for Javascript Calendar scripts to locate the textbox
        /// </summary>
        /// <returns>ClientID of txtDate</returns>
        public string getClientID()
        {
            return txtDate.ClientID;
        }

        [Category("Appearance")]
        [Description("Show Week Or not of the Popup window")]
        [Browsable(true)]
        public bool ShowWeek
        {
            get { return (ViewState["ShowWeek"] == null) ? false :(bool) ViewState["ShowWeek"]; }
            set { ViewState["ShowWeek"] = value; }
        }

        [Category("Appearance")]
        [Description("Style of DateTextBox")]
        [Browsable(true)]
        public DateTextBoxStyle ClickStyle
        {
            get { return (ViewState["ClickStyle"] == null) ? DateTextBoxStyle.WihtImgInside : (DateTextBoxStyle)ViewState["ClickStyle"]; }
            set
            {
                ViewState["ClickStyle"] = value;
               
            }
        }

        [Category("Appearance")]
        [Description("Dateformat apply to the text box.")]
        [Browsable(true)]
        public string DateFormat
        {
            get { return (ViewState["DateFormat"] == null) ? "yyyy-MM-dd" : ViewState["DateFormat"].ToString(); }
            set { ViewState["DateFormat"] = value; }
        }

        [Category("Appearance")]
        [Description("TWDate")]
        [Browsable(true)]
        private bool IsTWDate
        {
            get 
            {
                if (DateFormat.IndexOf("yyyy") < 0 && DateFormat.IndexOf("yyy") >= 0)
                {
                    
                    return true;
                }
                else
                {
                    return false;
                }
            }
            //set
            //{
            //    ViewState["IsTWDate"] = value;
            //}
        }

        /// <summary>
        /// Gets or sets a DateTime representation of the currently selected date.
        /// </summary>
        [Bindable(true, BindingDirection.OneWay)]
        [Browsable(true)]
        public DateTime CalendarDate
        {
            get
            {
                //if (IsTWDate)
                //{
                //    return DateTime.ParseExact(hfDate.Text, "yyyy-MM-dd HH:mm:ss", null);
                //}
                //else
                //{
                //    return DateTime.ParseExact(hfDate.Text, DateFormat, null);
                //}
                return DateTime.Parse(hfDate.Value);
            }
            set
            {
                if (IsTWDate)
                {
                    txtDate.Text = value.AddYears(-1911).ToString(DateFormat);
                }
                else
                {
                    txtDate.Text = value.ToString(DateFormat);
                }

                hfDate.Value = value.ToString("yyyy-MM-dd HH:mm:ss");


            }
        }

        /// <summary>
        /// Gets or sets a DateTime representation of the currently selected date.
        /// </summary>
        [Bindable(true, BindingDirection.OneWay)]
        [Browsable(true)]
        public string CalendarDateText
        {
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    CalendarDate = Convert.ToDateTime(value);
                }
                else
                {
                    this.txtDate.Text = "";
                }
            }
        }
        public int width
        {

            get { return width; }
            set { txtDate.Width = value; }
        }

        /// <summary>
        /// Clears the date.
        /// </summary>
        public void ClearDate()
        {
            txtDate.Text = "";
            hfDate.Value = "";
        }
    }
}